package com.glsc.ngateway.platform.service;

import com.glsc.ngateway.platform.aspect.JwtHelper;
import com.glsc.ngateway.common.api.common.enums.PlatformConstant;
import com.glsc.ngateway.common.api.platform.dto.LoginUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述: <br>
 *
 * @author gaojc
 * @date 2020/9/3 10:42
 */
@Service
public class LdapService {


    @Autowired
    private LdapTemplate ldapTemplate;


 /*   @Value("${spring.ldap.domainName}")
    private String ldapDomainName;


    *//*
     * SpringLdap配置。通过@Value注解读取之前配置文件中的值
     *//*
    @Value("${spring.ldap.urls}")
    private String ldapUrl;

    @Value("${spring.ldap.base}")
    private String ldapBase;

    @Value("${spring.ldap.domainName}")
    private String ldapUserDn;

    @Value("${spring.ldap.referral}")
    private String ldapReferral;

    @Value("${spring.ldap.username}")
    private String ldapUserName;

    @Value("${spring.ldap.password}")
    private String ldapUserPwd;*/


    public Boolean checkUserAuthority(String userName, String password) {

        EqualsFilter filter = new EqualsFilter(PlatformConstant.LDAP_OA_USR, userName);

        return ldapTemplate.authenticate(PlatformConstant.LDAP_OA, filter.toString(), password);
    }


    public Map<String, String> getUserToken(String userName, String systemId) {

        Map<String, String> tokenMap = new HashMap<>();
        String token = JwtHelper.generateToken(userName);
//        updateToken(userName,token,systemId, System.currentTimeMillis()/1000L + 60*60);

        tokenMap.put(PlatformConstant.TOKEN, token);
        tokenMap.put(PlatformConstant.TOKEN_EXPIRE_TIME, System.currentTimeMillis() / 1000L + 60 * 60 + "");
        return tokenMap;
    }


    public Boolean checkInterfaceParams(LoginUserDto userDto, String interfaceId) {


        Boolean flag = false;
        switch (interfaceId) {
            case PlatformConstant.LOGIN_CODE:
                if (userDto.getUserName() != null && userDto.getSystemId() != null && userDto.getPassword() != null) {
                    flag = true;
                }
                break;
            case PlatformConstant.LOGIN_PORTAL_CODE:
                if (userDto.getUserName() != null && userDto.getSystemId() != null && userDto.getPassword() != null && userDto.getTryCode() != null) {
                    flag = true;
                }
                break;
            case PlatformConstant.CHECK_TOKEN:
                if (userDto.getSystemId() != null && userDto.getToken() != null) {
                    flag = true;
                }
                break;
            case PlatformConstant.RENEW_TOKEN:
                if (userDto.getToken() != null && userDto.getSystemId() != null && userDto.getDelayHours() != null) {
                    flag = true;
                }
                break;
            case PlatformConstant.KICKOUT_TOKEN:
                if (userDto.getToken() != null && userDto.getSystemId() != null) {
                    flag = true;
                }
                break;
            default:
                break;
        }
        return flag;
    }


}
